﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QL_LichCT.BusinessLayer;

namespace QL_LichCT.PresentationLayer
{
    public partial class FrmXepLichHop : Form
    {
        public FrmXepLichHop()
        {
            InitializeComponent();
        }

        public void Load_cmbBoMon()
        {
            cmbBoMon.DataSource = BOMONBLL.GetList();
            cmbBoMon.DisplayMember = "tenbm";
            cmbBoMon.ValueMember = "mabm";
        }

        public void Load_cmbGiangVien()
        {
            string mabm = '\'' + cmbBoMon.SelectedValue.ToString() + '\'';
            cmbGiangVien.DataSource = GIANGVIENBLL.GetListByCoLumn("mabm", mabm);
            cmbGiangVien.DisplayMember = "tencb";
            cmbGiangVien.ValueMember = "macb";
        }

        private bool KT_CB(string macb)
        {
            if (dgvThemCB.RowCount > 1)
                for (int i = 0; i < dgvThemCB.RowCount - 1; i++ )
                {
                    if (macb == dgvThemCB.Rows[i].Cells[0].Value.ToString())
                        return true;
                }

            return false;
        }

        protected void ThemCanBo()
        {
            object magv = '\'' + cmbGiangVien.SelectedValue.ToString() + '\'';
            object macv = (object)('\'' + GIANGVIENBLL.GetColumnByTC("macv", "macb", magv).ToString() + '\'');
            object mabm = '\'' + cmbBoMon.SelectedValue.ToString() + '\'';

            string macb = GIANGVIENBLL.GetColumnByTC("macb", "macb", magv).ToString();
            string hoten = GIANGVIENBLL.GetColumnByTC("tencb", "macb", magv).ToString();
            string chucvu = CHUCVUBLL.GetColumnByTC("tencv", "macv", macv).ToString();
            string bomon = BOMONBLL.GetColumnByTC("tenbm", "mabm", mabm).ToString();
            
            int c = dgvThemCB.RowCount;
            
            this.dgvThemCB.Rows.Add();

            this.dgvThemCB.Rows[c-1].Cells[0].Value = macb;
            this.dgvThemCB.Rows[c-1].Cells[1].Value = hoten;
            this.dgvThemCB.Rows[c-1].Cells[2].Value = bomon;
            this.dgvThemCB.Rows[c-1].Cells[3].Value = chucvu;
        }

        private void cmbBoMon_SelectedIndexChanged(object sender, EventArgs e)
        {
            Load_cmbGiangVien();
        }

        private void FrmXepLichHop_Load(object sender, EventArgs e)
        {
            Load_cmbBoMon();
            Load_cmbGiangVien();
        }

        private void btnChon_Click(object sender, EventArgs e)
        {
            if (KT_CB(cmbGiangVien.SelectedValue.ToString()))
                MessageBox.Show("Cán bộ này đã có trong danh sách được chọn! Vui lòng xem lại.", "Lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error);
            else
                ThemCanBo();
        }
    }
}
